//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.cloudfast.common.sm2;

import java.io.UnsupportedEncodingException;

public class HexUtils {
    public static final String CHATSETUTF8 = "UTF-8";

    public HexUtils() {
    }

    public static String bytes2HexString(byte[] b) {
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < b.length; ++i) {
            result.append(String.format("%02X", b[i]));
        }

        return result.toString();
    }

    public static String encodeToLowerString(byte[] data) {
        char[] digital = "0123456789abcdef".toCharArray();
        StringBuilder sb = new StringBuilder("");

        for (int i = 0; i < data.length; ++i) {
            int bit = (data[i] & 240) >> 4;
            sb.append(digital[bit]);
            bit = data[i] & 15;
            sb.append(digital[bit]);
        }

        return sb.toString();
    }

    public static String encodeToUpperString(byte[] data) {
        char[] digital = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");

        for (int i = 0; i < data.length; ++i) {
            int bit = (data[i] & 240) >> 4;
            sb.append(digital[bit]);
            bit = data[i] & 15;
            sb.append(digital[bit]);
        }

        return sb.toString();
    }

    public static byte[] hexString2Bytes(String src) {
        int l = src.length() / 2;
        byte[] ret = new byte[l];

        for (int i = 0; i < l; ++i) {
            ret[i] = Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
        }

        return ret;
    }

    public static byte[] decode(String hex) {
        String digital = "0123456789abcdef";
        char[] hex2char = hex.toLowerCase().toCharArray();
        byte[] bytes = new byte[hex.length() / 2];

        for (int i = 0; i < bytes.length; ++i) {
            int temp = digital.indexOf(hex2char[2 * i]) << 4;
            temp += digital.indexOf(hex2char[2 * i + 1]);
            bytes[i] = (byte) (temp & 255);
        }

        return bytes;
    }

    public static byte[] hexToByte(String hex) {
        int two = 2;
        if (hex.length() % two != 0) {
            throw new IllegalArgumentException("error");
        } else {
            char[] arr = hex.toCharArray();
            byte[] b = new byte[hex.length() / two];
            int i = 0;
            int j = 0;

            for (int l = hex.length(); i < l; ++j) {
                String swap = "" + arr[i + 1] + arr[i];
                Integer byteInteger = Integer.parseInt(swap, 16) & 255;
                b[j] = byteInteger.byteValue();
                ++i;
            }

            return b;
        }
    }

    public static String byteToHex(byte[] b) {
        if (b == null) {
            throw new IllegalArgumentException("Argument b ( byte array ) is null! ");
        } else {
            StringBuilder hs = new StringBuilder();
            String stmp = "";

            for (int n = 0; n < b.length; ++n) {
                stmp = Integer.toHexString(b[n] & 255);
                if (stmp.length() == 1) {
                    hs.append("0").append(stmp);
                } else {
                    hs.append(stmp);
                }
            }

            return hs.toString().toLowerCase();
        }
    }

    public static String string2HexUtf8(String strPart) {
        return string2HexString(strPart, "UTF-8");
    }

    public static String string2HexString(String strPart, String tochartype) {
        try {
            return bytes2HexString(strPart.getBytes(tochartype));
        } catch (UnsupportedEncodingException var3) {
            return "";
        }
    }

    public static String hexUTF82String(String src) {
        return hexString2String(src, "UTF-8", "UTF-8");
    }

    public static String hexString2String(String src, String oldchartype, String chartype) {
        byte[] bts = hexString2Bytes(src);

        try {
            return oldchartype.equals(chartype) ? new String(bts, oldchartype)
                    : new String((new String(bts, oldchartype)).getBytes(), chartype);
        } catch (UnsupportedEncodingException var5) {
            return "";
        }
    }

    public String string2HexUTF16LE(String strPart) {
        return string2HexString(strPart, "UTF-16LE");
    }

    public String string2HexUnicode(String strPart) {
        return string2HexString(strPart, "Unicode");
    }

    public String string2HexGBK(String strPart) {
        return string2HexString(strPart, "GBK");
    }

    public String hexUTF16LE2String(String src) {
        return hexString2String(src, "UTF-16LE", "UTF-8");
    }

    public String hexGBK2String(String src) {
        return hexString2String(src, "GBK", "UTF-8");
    }

    public String hexUnicode2String(String src) {
        return hexString2String(src, "Unicode", "UTF-8");
    }
}
